<?php
# MantisBT - a php based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.

	/**
	 * @package MantisBT
	 * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	 * @copyright Copyright (C) 2002 - 2010  MantisBT Team - mantisbt-dev@lists.sourceforge.net
	 * @link http://www.mantisbt.org
	 */
	 /**
	  * MantisBT Core API's
	  */
	require_once( 'core.php' );
	require_once( 'current_user_api.php' );
	require_once( 'compress_api.php' );
	require_once( 'filter_api.php' );
	require_once( 'last_visited_api.php' );

	auth_ensure_user_authenticated();

	$t_current_user_name = current_user_get_field( 'username' );
	$t_current_real_name = current_user_get_field( 'realname' );
	
	# Improve performance by caching category data in one pass
	category_get_all_rows( helper_get_current_project() );

	compress_enable();

	# don't index my view page
	html_robots_noindex();

	html_page_top1( lang_get( 'my_view_link' ) );

	if ( current_user_get_pref( 'refresh_delay' ) > 0 ) {
		html_meta_redirect( 'my_view_page.php', current_user_get_pref( 'refresh_delay' )*60 );
	}

	html_page_top2();

	print_recently_visited();

	$f_page_number		= gpc_get_int( 'page_number', 1 );

	$t_per_page = config_get( 'my_view_bug_count' );
	$t_bug_count = null;
	$t_page_count = null;

	$t_boxes = config_get( 'my_view_boxes' );
	asort ($t_boxes);
	reset ($t_boxes);
	#print_r ($t_boxes);

	$t_project_id = helper_get_current_project();
?>
<?php 

$do = $_GET['do'];

if ($do == search)
{
echo '<br>
Trung tâm Dịch vụ khách hàng<br>
================================================================================================
					<br><br><br>
					<form action="coogle.php?do=sc" method="POST" name="searching">
										<b>Tìm kiếm theo user:</b>
										<Select name="search">
										<option value=fspcode>Tìm theo mã khách hàng (Ex: 158)</option>
										<option value=cphone>Tìm theo số điện thoại (Ex: 091234567)</option>
										<option value=cname>Tìm theo họ tên (Ex: nguyenvan a)</option>
										<option value=cmail>Tìm theo email (Ex: abc@gmail.com)</option>
										<option value=cstatus>Tìm theo status (Ex: incoming)</option>
										<option value=cssupport>Tìm theo SupportName (Ex: nhungtth)</option>
										</select>
										<input name="soption" type="soption">
										<input name="submit" type="submit" value="LookUp">
									</form>';
echo '<br><br>
		<form action="?do=sdate" method="POST" name="sdate">
		<table>
		<tr><td>Tìm theo ngày/tháng/năm (Ex:15/9/2011)</td><td>
			<input name="sdate" type="sdate" style="width:30px">
			- <input name="smonth" type="smonth"  style="width:30px">
			- <input name="syear" type="syear"  style="width:30px"> 
			<input name="submit" type="submit" value="LookUp"></td><td></td></tr>
		</table>
		</form>
		</table>';
}

	if ($do == sc)
	{
	$search = $_POST['search'];
	$soption = $_POST['soption'];
	$trimmed = trim($soption);
	if (empty($trimmed))
	{
		echo "Nhập thông tin cần nhập đi";
	}
	else
	{
		//Tim theo name
		echo '<br><br>
		<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  
		<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=2%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>Tên khách hàng</td>
										<td width=8%>sTime</td>
										<td width=24%>Phản hồi/Xử lí</td>
										<td width=7%>Trạng thái</td>
										<td width=7%>Category</td>
										<td width=7%>uTime</td>
										<td width=7%>Người khởi tạo</td>
										<td width=7%>Người xử lí cuối</td>
									</tr>';
		if ($search == cname)
		{		
			$que = mysql_query("Select * from `mantis_care_report_table` where name like \"%$trimmed%\" ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{	
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
												// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td class='.$state.'>'.$i.'</td>
										<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
										<td class='.$state.'>'.$start_time.'</td>
										<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
										<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td class='.$state.'>'.$object.'</td>
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			} 
		}
		
		//Tim theo phone
		if ($search == cphone)
		{
			$que = mysql_query("Select * from `mantis_care_report_table` where phone like \"%$trimmed%\" ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{
				$i++;
				$id	= $row['id'];
				$name = $row['name'];
				$phone = $row['phone'];
				$email = $row['email'];
				$sex = $row['sex'];
				$address = $row['address'];
				$city = $row['city'];
				$serial = $row['serial'];
				$lo = $row['lo'];
				$object = $row['object'];
				$status = $row['status'];
				$state = $row['state'];
				$confirm = $row['confirm'];
				$process = $row['process'];
				$receiver = $row['receiver'];
				$carer = $row['carer'];
				$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
				// Max a last update
				$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
				$resultt = mysql_query($qr) or die(mysql_error());
				$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
				$resultqt = mysql_query($qt) or die(mysql_error());
				// Print out result
				
				echo '<tr>
				<td class='.$state.'>'.$i.'</td>
				<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
				<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
				<td class='.$state.'>'.$start_time.'</td>
				<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
				<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
				<td class='.$state.'>'.$object.'</td>
				<td class='.$state.'>';
					while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			}
		}
		
		//Tim theo mail
		if ($search == cmail)
		{
			$que = mysql_query("Select * from `mantis_care_report_table` where email like \"%$trimmed%\" ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{
				$i++;
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
												// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td class='.$state.'>'.$i.'</td>
										<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
										<td class='.$state.'>'.$start_time.'</td>
										<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
										<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td class='.$state.'>'.$object.'</td>
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			}
		}
		
		//Tim theo Cate
		if ($search == fspcode)
		{
			$que = mysql_query("Select * from `mantis_care_report_table` where spcode like \"%$trimmed%\" ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
												// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td class='.$state.'>'.$i.'</td>
										<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
										<td class='.$state.'>'.$start_time.'</td>
										<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
										<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td class='.$state.'>'.$object.'</td>
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			}
		}
		
		//Tim theo Status
		if ($search == cstatus)
		{
			$que = mysql_query("Select * from `mantis_care_report_table` where status='$trimmed' ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{
				$i++;
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
												// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td class='.$state.'>'.$i.'</td>
										<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
										<td class='.$state.'>'.$start_time.'</td>
										<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
										<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td class='.$state.'>'.$object.'</td>
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			}
		}
		
		//Tim theo Start support
		if ($search == cssupport)
		{
			$que = mysql_query("Select * from `mantis_care_report_table` where carer like \"%$trimmed%\" ORDER BY id DESC");
			while ($row = mysql_fetch_array($que))
			{
				$i++;
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
												// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td class='.$state.'>'.$i.'</td>
										<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
										<td class='.$state.'>'.$start_time.'</td>
										<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
										<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td class='.$state.'>'.$object.'</td>
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td class='.$state.'>'.$carer.'</td>					
										<td class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{	$resolver = $row['resolver'];
																echo $resolver;
															}	echo'</td>
										</tr>';	
			}
		}
		echo '</table>';
	}
	}

	if (($do == xreport) and ($_GET['value'] == x))
		{
			
			$sDay = $_POST['sDay'];
			$sMonth = $_POST['sMonth'];
			$sYear = $_POST['sYear'];
			$eDay = $_POST['eDay'];
			$eMonth = $_POST['eMonth'];
			$eYear = $_POST['eYear'];
			$username = $_POST['cNameAgent'];
			echo '<br>
				<input type="button" onClick="window.print()" value="In báo cáo"/>
				<br><br>
				<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% > 
						<tr>
							<td>   
								<table border=1 bordercolor=black cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%><a href="">NAME</a></td>
										<td width=10%><a href="">CATEGORY</a></td>
										<td width=7%><a href="">STATUS</a></td>
										<td width=7%>Start time</td>
										<td width=7%>Start Supporter</td>
										<td width=20%>Phản hồi</td>
										<td width=20%>Xử lí</td>
									</tr>
								</table>
								<table border=1 bordercolor=black cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>			
								';
							$i = 0;
							$er = current_user_get_field( 'access_level' );
					if ($er <90)
					{
							$CheckReportOfMonth = mysql_query("select * from `mantis_care_report_table` WHERE carer='$t_current_user_name' 
									AND from_unixtime(start_time, '%e') >='$sDay' AND from_unixtime(start_time, '%e')<='$eDay'
									AND from_unixtime(start_time, '%c') >='$sMonth' AND from_unixtime(start_time, '%c')<='$eMonth'
									AND from_unixtime(start_time, '%Y') >='$sYear' AND from_unixtime(start_time, '%Y')<='$eYear'
									ORDER By id ASC
							");
					}
					else 
					{
							$CheckReportOfMonth = mysql_query("select * from `mantis_care_report_table` WHERE carer='$username' 
									AND from_unixtime(start_time, '%e') >='$sDay' AND from_unixtime(start_time, '%e')<='$eDay'
									AND from_unixtime(start_time, '%c') >='$sMonth' AND from_unixtime(start_time, '%c')<='$eMonth'
									AND from_unixtime(start_time, '%Y') >='$sYear' AND from_unixtime(start_time, '%Y')<='$eYear'
									ORDER By id ASC
							");
					}
					
					while ($row = mysql_fetch_array($CheckReportOfMonth))
					{
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									$QueryTime = (date("H",$row['start_time']));
									$QueryDay = (date("d",$row['start_time']));
									$QueryMonth = (date("m",$row['start_time']));
									$QueryYear = (date("y",$row['start_time']));
								
							// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
								echo '<tr>
										<td width=4%   class='.$state.'>'.$i.'</td>
										<td width=4%   class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td width=10%  class='.$state.'>'.$name.'</td>
										<td width=10%  class='.$state.'>['.$status.']<br>'.$object.'</td>
										<td width=7%  class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%   class='.$state.'>'.$start_time.'</td>
										<td width=7%   class='.$state.'>'.$carer.'</td>
										<td width=20%  class='.$state.'c>'.nl2br(substr($confirm,0,300)).'</td>
										<td width=20%  class='.$state.'c>'.nl2br(substr($process,0,300)).'</td>
										</tr>';	
					} 
				
						echo '</table>';
				echo '<div align=center width=50%>
				<table width=80% height=200 border=1>
				<tr>
					<td width=50% class=sign valign=top><br><br><b>Người báo cáo<br><br><br><br><br><br><br><br><br>'.$t_current_real_name.'</td>
					<td width=50% class=sign valign=top><br><br><b>Người xác nhận<br> GD Trung Tâm DVKH<br><br><br><br><br><br><br><br>Lương Vũ An Bình</td>
				</tr>
				</table>
				</div>';
				
		}

	if ($do == sdate)
	{
	
		echo '<br><br>
		<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  
		<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=2%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>Tên khách hàng</td>
										<td width=8%>sTime</td>
										<td width=24%>Phản hồi/Xử lí</td>
										<td width=7%>Trạng thái</td>
										<td width=7%>Category</td>
										<td width=7%>uTime</td>
										<td width=7%>Người khởi tạo</td>
										<td width=7%>Người xử lí cuối</td>
									</tr>';
		$sdate = $_POST['sdate'];
		$smonth = $_POST['smonth'];
		$syear = $_POST['syear'];
		
		$qrdate = mysql_query("
			SELECT * FROM `mantis_care_report_table` 
			WHERE from_unixtime(start_time, '%e') = '$sdate' and 
			from_unixtime(start_time, '%c') = '$smonth' and
			from_unixtime(start_time, '%Y') = '$syear'
			ORDER BY id DESC");
		
		While ($row = mysql_fetch_array($qrdate))
		{
				$i++;
				$id	= $row['id'];
				$name = $row['name'];
				$phone = $row['phone'];
				$email = $row['email'];
				$sex = $row['sex'];
				$address = $row['address'];
				$city = $row['city'];
				$serial = $row['serial'];
				$lo = $row['lo'];
				$object = $row['object'];
				$status = $row['status'];
				$state = $row['state'];
				$confirm = $row['confirm'];
				$process = $row['process'];
				$receiver = $row['receiver'];
				$carer = $row['carer'];
				$start_time = (date("d-m-Y H:i:s",$row['start_time']));	
				
				// Max a last update
				$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
				$resultt = mysql_query($qr) or die(mysql_error());
				$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
				$resultqt = mysql_query($qt) or die(mysql_error());
				// Print out result
				
				echo '<tr>
				<td class='.$state.'>'.$i.'</td>
				<td class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
				<td class='.$state.'>'.$name.'<br>[Phone: '.$phone.']</td>
				<td class='.$state.'>'.$start_time.'</td>
				<td class='.$state.'><b>Phản hồi:<br></b>'.$confirm.'<br><b>Xử lí:</b><br>'.$process.'</td>
				<td class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
				<td class='.$state.'>'.$object.'</td>
				<td class='.$state.'>';
					while($row = mysql_fetch_array($resultt))
							{
							$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
							echo $last_updated;
							}	echo '</td>
				<td class='.$state.'>'.$carer.'</td>					
				<td class='.$state.'>';
					while($row = mysql_fetch_array($resultqt))
							{	$resolver = $row['resolver'];
							echo $resolver;
							}	echo'</td>
				</tr>';	
		}
		echo '</table>';
	}
?>


<?php
	html_page_bottom();
